package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.entity.SettingFormType;
import com.github.aloxc.plugin.restplus.entity.SettingListenerContext;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.listener.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;

/**
 * 设置窗口
 * @author leero 
 */
public class SettingForm extends JBPanel implements ISettingPanel {
    private JPanel rootPanel;
    private JPanel settingPane;
    private JPanel rightPane;
    private JButton globalButton;
    private JPanel leftPane;
    private JButton environmentButton;

    public JButton getYapiButton() {
        return yapiButton;
    }

    private JButton yapiButton;
    private JButton helpButton;
    private JButton donateButton;

    private Dialog dialog;
    private Project project;

    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }

    public SettingForm(Project project){
        super();
        this.project = project;
        initComponent();

        int height = 640;
        int width = 880;
        this.setMaximumSize(new Dimension(width,height));
        this.setMinimumSize(new Dimension(width,height));
        this.setPreferredSize(new Dimension(width,height));

        com.github.aloxc.plugin.restplus.component.Dialog dialog = new Dialog(null, this,
                new Dimension(width,height),
                new Dimension(width,height),
                new Dimension(width,height),
                IconLoader.getIcon("/icons/setting.png"),
                I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.global.title")
        );
        this.setDialog(dialog);
//        dialog.setVisible(true);
    }
    public Dialog getDialog() {
        return dialog;
    }


    public JButton getEnvironmentButton() {
        return environmentButton;
    }


    public JPanel getRightPane() {
        return rightPane;
    }

    public Project getProject() {
        return project;
    }


    private void initComponent() {
        initUi();
        initActions();
    }
    private void initUi() {
        GridLayoutManager settingLayoutManager = new GridLayoutManager(1, 2);
        settingLayoutManager.setHGap(1);
        settingLayoutManager.setVGap(1);
        settingLayoutManager.setMargin(new Insets(20,20,20,20));
        settingPane = new JBPanel<>();
        settingPane.setLayout(settingLayoutManager);
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(1, 1));
        initLeftPanel();
        initRightPanel();
        initPanel();
        this.globalButton.setToolTipText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.global.title"));
        this.environmentButton.setToolTipText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.title"));
        this.yapiButton.setToolTipText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.yapi.title"));
        this.helpButton.setToolTipText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.toolbar.help.text"));
        this.donateButton.setToolTipText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.toolbar.donate.text"));
    }

    private void initPanel() {
        settingPane.add(leftPane,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        new Dimension(32,-1), new Dimension(32,-1), new Dimension(32,-1)));
        Dimension size460 = new Dimension(805, 570);
        settingPane.add(rightPane,
                new GridConstraints(0, 1, 1, 1,
                        GridConstraints.ANCHOR_NORTHWEST,
                        GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_BOTH,
                        size460, size460, size460));
        this.add(settingPane,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_BOTH,
                        null, null, null));
    }


    private void initRightPanel() {
        GridLayoutManager rightPaneLayoutManager = new GridLayoutManager(1, 1);
        rightPaneLayoutManager.setHGap(1);
        rightPaneLayoutManager.setVGap(1);
        rightPaneLayoutManager.setMargin(new Insets(0,10,10,0));
        rightPane = new JBPanel<>();
        rightPane.setLayout(rightPaneLayoutManager);
    }

    private void initLeftPanel() {
        LayoutManager flowLayoutManager = new FlowLayout();
        leftPane = new JBPanel<>();
        leftPane.setLayout(flowLayoutManager);
        globalButton.setBorder(BorderFactory.createRaisedBevelBorder());
        globalButton.setBorderPainted(false);
        globalButton.setOpaque(false);
        globalButton.setRolloverEnabled(true);
        globalButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        environmentButton.setFocusPainted(true);
        environmentButton.setBorder(BorderFactory.createRaisedBevelBorder());
        environmentButton.setBorderPainted(false);
        environmentButton.setOpaque(false);
        environmentButton.setRolloverEnabled(true);
        environmentButton.setFocusPainted(true);
        environmentButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));


        yapiButton.setFocusPainted(true);
        yapiButton.setBorder(BorderFactory.createRaisedBevelBorder());
        yapiButton.setBorderPainted(false);
        yapiButton.setOpaque(false);
        yapiButton.setRolloverEnabled(true);
        yapiButton.setFocusPainted(true);
        yapiButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        helpButton.setFocusPainted(true);
        helpButton.setBorder(BorderFactory.createRaisedBevelBorder());
        helpButton.setBorderPainted(false);
        helpButton.setOpaque(false);
        helpButton.setRolloverEnabled(true);
        helpButton.setFocusPainted(true);
        helpButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        donateButton.setFocusPainted(true);
        donateButton.setBorder(BorderFactory.createRaisedBevelBorder());
        donateButton.setBorderPainted(false);
        donateButton.setOpaque(false);
        donateButton.setRolloverEnabled(true);
        donateButton.setFocusPainted(true);
        donateButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        leftPane.add(globalButton,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        new Dimension(32,32), new Dimension(32,32),  new Dimension(32,32)));
        leftPane.add(environmentButton,
                new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        new Dimension(32,32), new Dimension(32,32),  new Dimension(32,32)));
        leftPane.add(yapiButton,
                new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        new Dimension(32,32), new Dimension(32,32),  new Dimension(32,32)));
        leftPane.add(helpButton,
                new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        new Dimension(32,32), new Dimension(32,32),  new Dimension(32,32)));
//        leftPane.add(donateButton,
//                new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
//                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
//                        new Dimension(32,32), new Dimension(32,32),  new Dimension(32,32)));
    }
    private void initActions() {
        globalButton.addActionListener(new SettingListener(this,new SettingListenerContext(SettingFormType.GLOBAL,null) ));
        environmentButton.addMouseListener(new EnvironmentSettingMouseListener(this));
        yapiButton.addActionListener(new YapiSettingMouseListener(this));
        helpButton.addActionListener(new HelpMouseListener(this));
        donateButton.addActionListener(new DonateMouseListener(this));
    }

    @Override
    public void setDialog(Dialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void setProject(Project project) {
        this.project = project;
    }

}
